import { ConfigProvider } from "antd";
import zh_CN from "antd/es/locale/zh_CN";
import React from "react";
import { Provider } from "react-redux";
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
import { applyMiddleware, combineReducers, compose, createStore } from "redux";
import thunkMiddleWare from "redux-thunk";
import * as commonReducer from "./common/store/reducer";
import LoginModal from "./common/components/loginModal";
import { MyAxios } from "./common/config/http";
import { PagePath } from "./common/config/page";
import HomePage from "./common/layout/home";
import * as manageOrderReducer from "./pages/manage/order/store/reducer";
import Login from "./common/pages/login";
import Registe from "./common/pages/registe";
import ResetPwd from "./common/pages/reset_pwd";
import * as manageUserReducer from "./pages/manage/user/store/reducer";
import * as manageVpsReducer from "./pages/manage/vps/store/reducer";
import * as manageAreaReducer from "./pages/manage/area/store/reducer";
import * as userOrderReducer from "./pages/user/order/store/reducer";

// 定义所有的reducer,类型为reducer的State类型
export interface Reducers {
  // reducer: number;
  manageUserReducer: typeof manageUserReducer.defaultState;
  commonReducer: typeof commonReducer.defaultState;
  manageOrderReducer: typeof manageOrderReducer.defaultState;
  manageVpsReducer: typeof manageVpsReducer.defaultState;
  manageAreaReducer: typeof manageAreaReducer.defaultState;
  userOrderReducer: typeof userOrderReducer.defaultState;
}

const rootReducer = combineReducers({
  manageUserReducer: manageUserReducer.default,
  commonReducer: commonReducer.default,
  manageOrderReducer: manageOrderReducer.default,
  manageVpsReducer: manageVpsReducer.default,
  manageAreaReducer: manageAreaReducer.default,
  userOrderReducer: userOrderReducer.default
});

// 获取window对象
const myWindow: any = window;
// 这段是为了让chrome浏览器的redux插件生效,正式部署时去掉
const composeEnhancers = myWindow.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
  ? myWindow.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({})
  : compose;
// 开发版的仓库(浏览器插件生效)
export const store = createStore(
  rootReducer,
  composeEnhancers(applyMiddleware(thunkMiddleWare))
);
// 正式版的仓库
// const store = createStore(rootReducer, applyMiddleware(thunkMiddleWare));

//实例化MyAxios,否则无法获取axios实例
new MyAxios();

function App() {
  return (
    // 设置antd组件的语言为`中文`
    <ConfigProvider locale={zh_CN}>
      <Provider store={store}>
        <Router>
          <LoginModal />
          <Switch>
            // 从上往下优先匹配路由
            <Route path={PagePath.LOGIN} component={Login} />
            <Route path={PagePath.REGISTE} component={Registe} />
            <Route path={PagePath.RESET_PWD} component={ResetPwd} />
            <Route path={PagePath.ROOT} component={HomePage} />
          </Switch>
        </Router>
      </Provider>
    </ConfigProvider>
  );
}
export default App;
